$(function () {
    //票据类型
    $("#form_tpfk_pj").find("[idVal='tpfk_pjlx_id']").sjzdcombobox("pjlx");

    //申报公司对申报部门申报人的级联操作
    $("#tpfk_sbgs_id").jgbmtree($("#tpfk_sbbm_id"),$("#tpfk_sbr_id"),$("#tpfk_sbbm_id").val(),$("#tpfk_sbr_id").val());

    //
    //删除电子借款单的按钮触发
    $("#btn_pj_del").click(function () {
        var pjRow = $('#tpfk_pj_grid').edatagrid('getSelected');
        var pjIndex = $('#tpfk_pj_grid').edatagrid('getRowIndex',pjRow);
        if(pjRow){
            $('#tpfk_pj_grid').edatagrid('deleteRow',pjIndex);
            countSumPj();
        } else {
            $.messager.alert('提示','请选择要删除的票据!');
        }
    });

    var fkId = $("#skf_fk_id").val();
    if(fkId==""){
        fkId = 0;
    }
    $("#tpfk_pj_grid").datagrid({
        url:$.webPath() + "/zjgl/pjgl/pjglList?pjsflxType=1&&sfkId="+fkId,
        fitColumns: true,
        singleSelect: true,
        width:730,
        height:200,
        checkOnSelect: false,
        selectOnCheck: false,
        showFooter:true,
        rownumbers: true,
        columns: [
            [
                {field:'id', title:'ID', hidden:true},
                {field:'pjhm', title:'票据号码', width:20,align: 'center'},
                {field:'dqje', title:'票据金额', width:20, formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                },align: 'right'},
                {field:'dqr', title:'到期日', width:20,align: 'center'},
                {field:'bz', title:'备注', width:20,align: 'center'}
            ]
        ]
    })

    //搜索票据
    $("#btn_pj_search").click(function () {
        searcPjhwindow(tpfk_pj_searchWindowOKbtnHandler);
    });
});


//查询销售单确定按钮回调函数
function tpfk_pj_searchWindowOKbtnHandler(data) {
    $.each(data, function () {
        $("#tpfk_pj_grid").edatagrid('appendRow', {
            'id': this.id,
            'pjhm': this.pjhm,
            'dqje': this.dqje,
            'dqr': this.dqr,
            'bz': this.bz
        });
        countSumPj();
    });
}

//计算计划总量
function countSumPj() {
    var sumdqje = 0;
    var data = $("#tpfk_pj_grid").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var dqje=parseFloat(data.rows[i]['dqje']);
        dqje = isNaN(dqje) ? 0 : dqje;
        sumdqje += dqje;
    }
    addFooter(sumdqje);
}

function addFooter(sumdqje) {
    var footer = null;
    footer = [{"spr_jc": "合计", "dqje": formatMoney(sumdqje)}];
    if (footer != null)
        $("#tpfk_pj_grid").edatagrid('reloadFooter',footer);
}
